package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.logging.log4j.message.StringFormattedMessage;
import org.example.exception.AppException;
import org.example.exception.AppExceptionCodeMsg;
import org.example.pojo.Cost;
import org.example.pojo.Prescription;
import org.example.query.PrescriptionQuery;
import org.example.service.ICostService;
import org.example.service.IPrescriptionService;
import org.example.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-18
 */

/**
 * 再添加处方的时候也要添加在费用表中进行添加数据
 * 所以这里需要用到数据回滚
 */
@RestController
@RequestMapping("/prescription")
public class PrescriptionController extends BaseController <Prescription, PrescriptionQuery> {

    @Autowired
    private IPrescriptionService prescriptionService;
    @Autowired
    private ICostService costService;
    @PostMapping("/conditions")
    public ResultBean getByConditions(@RequestBody PrescriptionQuery query) {
        List list = prescriptionService.list(query.getJoinQueryWrapper());
        return ResultBean.SUCCESS(list);
    }
    @Transactional
    @PostMapping("/addbatch")
    public ResultBean addbatch(@RequestBody List<Prescription> prescriptions){
        if( prescriptions==null || prescriptions.size()==0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        Boolean b=prescriptionService.saveBatch(prescriptions);
        if(!b) {
            throw new AppException(600001,"开药失败");
        }

        //然后开始添加费用
        List<Cost> list = new ArrayList<>();

        prescriptions.forEach(prescription -> {
            Cost cost = new Cost(null, prescription.getRegisterId(),
                    prescription.getDrugInfo().getDrugName(),
                    prescription.getId(),
                    "药品", prescription.getDrugInfo().getDrugPrice(),
                    prescription.getDrugNumber(), null, 0, null, null, null, null);
            list.add(cost);
        });
        Boolean b1=costService.saveBatch(list);
        if(!b1) {
            throw new AppException(600002,"添加费用失败");
        }
        return ResultBean.SUCCESS();
    }
    //这里传了有两个参数，一个是字符串，一个是数组，字符串是通过地址栏的形式传过去的，而数组是通过请求体传过去的
    @PutMapping("/updatestate/{state}")
    public ResultBean updatestate(@PathVariable("state")String state,@RequestBody List<Integer> ids) {
        //点击批量发药会触发   执行发药  本质上就是修改发药信息的状态值  update prescription drug_state='已发药' where id in (1,2,3,4)
        //这里没有联合查询，只是一个表在进行查询
        UpdateWrapper<Prescription> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("drug_state", state)
                .in("id", ids);
        boolean update = prescriptionService.update(updateWrapper);
        return update?ResultBean.SUCCESS():ResultBean.ERROR("更新发药状态失败");
    }
}
